//加载页面
$(document).ready(function () {
    var id = $("#id").val();
    if (id) {
        $.ajax({
            url: url_config.gateway_business_system_find_by_id,
            type: 'POST',
            dataType: 'json',
            data: {id: id}
        })
            .done(function (result) {
                serviceValidateErrorTipsMessage(result);

                if (result.code == "20000" && result.data) {
                    $("#systemName").val(result.data.systemName);
                    $("#systemCode").val(result.data.systemCode).attr("disabled", true);
                    $("#systemStatus").val(result.data.systemStatus);
                    $("#detailExplain").val(result.data.detailExplain);
                }

            });

    }

    $("#generalForm").bootstrapValidator({
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            systemName: {
                validators: {
                    notEmpty: {
                        message: '系统名称不能为空!'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: '系统名长度必须在6到30之间!'
                    }
                }
            },
            systemCode: {
                validators: {
                    notEmpty: {
                        message: '系统编码不能为空!'
                    }
                }
            },
            systemStatus: {
                validators: {
                    notEmpty: {
                        message: '系统状态不能为空!'
                    }
                }
            }

        }
    });

});


function setPostData() {
    return {
        id: $("#id").val(),
        systemName: $("#systemName").val(),
        systemCode: $("#systemCode").val(),
        systemStatus: $("#systemStatus").val(),
        detailExplain: $("#detailExplain").val()
    }
}


//提交
$('#btnUpdate').click(function () {
    //提交时validate数据是否通过bootstrapValidator的校验
    var bootstrapValidator = $("#generalForm").data('bootstrapValidator');
    bootstrapValidator.validate();
    if (bootstrapValidator.isValid()) {
        $.ajax({
            url: url_config.gateway_business_system_addAndUpdate,
            type: 'POST',
            dataType: 'json',
            data: setPostData()
        })
            .done(function (result) {
                serviceValidateHandleCurrent(result, true, false);
                reload();
            });
    }
    else return false;
});


//取消
$('#btnCancel').click(function () {
    parent.layer.closeAll('iframe');
});
